package org.jtao.gbsubject.web.rest;

import com.codahale.metrics.annotation.Timed;
import org.jtao.gbsubject.service.SubjectClazzService;
import org.jtao.gbsubject.web.rest.errors.BadRequestAlertException;
import org.jtao.gbsubject.web.rest.util.HeaderUtil;
import org.jtao.gbsubject.web.rest.util.PaginationUtil;
import org.jtao.gbsubject.service.dto.SubjectClazzDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing SubjectClazz.
 */
@RestController
@RequestMapping("/api")
public class SubjectClazzResource {

    private final Logger log = LoggerFactory.getLogger(SubjectClazzResource.class);

    private static final String ENTITY_NAME = "subjectClazz";

    private final SubjectClazzService subjectClazzService;

    public SubjectClazzResource(SubjectClazzService subjectClazzService) {
        this.subjectClazzService = subjectClazzService;
    }

    /**
     * POST  /subject-clazzes : Create a new subjectClazz.
     *
     * @param subjectClazzDTO the subjectClazzDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new subjectClazzDTO, or with status 400 (Bad Request) if the subjectClazz has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/subject-clazzes")
    @Timed
    public ResponseEntity<SubjectClazzDTO> createSubjectClazz(@RequestBody SubjectClazzDTO subjectClazzDTO) throws URISyntaxException {
        log.debug("REST request to save SubjectClazz : {}", subjectClazzDTO);
        if (subjectClazzDTO.getId() != null) {
            throw new BadRequestAlertException("A new subjectClazz cannot already have an ID", ENTITY_NAME, "idexists");
        }
        SubjectClazzDTO result = subjectClazzService.save(subjectClazzDTO);
        return ResponseEntity.created(new URI("/api/subject-clazzes/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    @PostMapping("/subject-clazzes/import")
    @Timed
    public ResponseEntity<Integer> importSubjectClazz(@RequestParam("file") MultipartFile file) {
        log.debug("REST request to import SubjectClazz : {} ",file.getName());
        Integer sum=subjectClazzService.importSubjectClazz(file);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityImportAlert(ENTITY_NAME,sum.toString()))
            .body(sum);
    }

    /**
     * PUT  /subject-clazzes : Updates an existing subjectClazz.
     *
     * @param subjectClazzDTO the subjectClazzDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated subjectClazzDTO,
     * or with status 400 (Bad Request) if the subjectClazzDTO is not valid,
     * or with status 500 (Internal Server Error) if the subjectClazzDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/subject-clazzes")
    @Timed
    public ResponseEntity<SubjectClazzDTO> updateSubjectClazz(@RequestBody SubjectClazzDTO subjectClazzDTO) throws URISyntaxException {
        log.debug("REST request to update SubjectClazz : {}", subjectClazzDTO);
        if (subjectClazzDTO.getId() == null) {
            return createSubjectClazz(subjectClazzDTO);
        }
        SubjectClazzDTO result = subjectClazzService.save(subjectClazzDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, subjectClazzDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /subject-clazzes : get all the subjectClazzes.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of subjectClazzes in body
     */
    @GetMapping("/subject-clazzes")
    @Timed
    public ResponseEntity<List<SubjectClazzDTO>> getAllSubjectClazzes(Pageable pageable) {
        log.debug("REST request to get a page of SubjectClazzes");
        Page<SubjectClazzDTO> page = subjectClazzService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/subject-clazzes");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /subject-clazzes/:id : get the "id" subjectClazz.
     *
     * @param id the id of the subjectClazzDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the subjectClazzDTO, or with status 404 (Not Found)
     */
    @GetMapping("/subject-clazzes/{id}")
    @Timed
    public ResponseEntity<SubjectClazzDTO> getSubjectClazz(@PathVariable Long id) {
        log.debug("REST request to get SubjectClazz : {}", id);
        SubjectClazzDTO subjectClazzDTO = subjectClazzService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(subjectClazzDTO));
    }

    /**
     * DELETE  /subject-clazzes/:id : delete the "id" subjectClazz.
     *
     * @param id the id of the subjectClazzDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/subject-clazzes/{id}")
    @Timed
    public ResponseEntity<Void> deleteSubjectClazz(@PathVariable Long id) {
        log.debug("REST request to delete SubjectClazz : {}", id);
        subjectClazzService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
